package com.wudl.govern.config;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ：wudl
 * @date ：Created in 2022-04-05 13:31
 * @description：
 * @modified By：
 * @version: 1.0
 */
@Component
@Slf4j
public class DataDictUtils {


    public String getObjByIds(Set deptIds,int type) {
        log.info("对象ids集合参数: "+deptIds.toString());
        Map<String, Object> params = new HashMap<>(1);
        params.put("ids", deptIds);
        log.info("查询参数params:"+params);
        Object result="";
        switch (type){
            //用户
            case 1:
//                result = commonFeignDataService.getUserInfoByIds(params);
                break;
            //部门
            case 2:
//                result = commonFeignDataService.getDeptInfoByIds(params);
                break;
            //主题
            default:
//                result = commonFeignDataService.getDictInfoByIds(params);
                break;
        }
        return JSON.toJSONString(result);
    }


    /**
     * 渲染用户
     * @param object  对象集合
     * @param type 1:用户信息 2:部门 3:主题 4:行业 5:接口模块
     * @return
     */
    public List<Map> fillMap(Object object, int type) {
        if(null == object){
            return null;
        }
        List<Map<String, Object>> resultList;
        if(object instanceof List){
            resultList = (List<Map<String, Object>>) object;
        }else {
            resultList = new ArrayList<>();
            resultList.add((Map<String,Object>)object);
        }
        String target;
        String current;
        switch (type){
            //用户
            case 1:
                target = "userName";
                current = "createUser";
                break;
            //部门
            case 2:
                target = "deptName";
                current = "deptId";
                break;
            //主题
            case 3:
                target = "name";
                current = "themeId";
                break;
            //行业
            case 4:
                target = "name";
                current = "industryId";
                break;
            //接口模块
            default:
                target = "name";
                current = "moduleName";
                break;
        }
        //查询用户id集合
        Set<Object> idList = resultList.stream().map(ct -> ct.get(current)).collect(Collectors.toSet());
        String resultUserInfo = getObjByIds(idList, type);

        if(StringUtil.isEmpty(resultUserInfo) || JSON.parseObject(resultUserInfo).getInteger("code") != 100 ){
            log.info("根据集合id集合获取信息服务调用异常");
            return null;
        }
        List<Map> userList = JSON.parseArray(JSON.parseObject(resultUserInfo).getString("data"),Map.class);

        Map<String,Object> distinctMap = new HashMap<>(16);
        for (Map dictMap: userList){
            distinctMap.put(dictMap.get("id").toString(),dictMap.get("deptName"));
        }
        List<Map> resultCollect = resultList.stream().map(result -> {
            if (Objects.nonNull(distinctMap.get(result.get(current)))) {
                result.put("name",distinctMap.get(result.get(current)).toString());
            }
            return result;
        }).collect(Collectors.toList());
        return resultCollect;
    }

}
